<?php
/**
 * Autor:   Miguel A. Zilli
 * Email:   miguelzilli@gmail.com
 */

class InstalacionestiposController extends AppController{
    var $name='Instalacionestipos';
    var $uses=array('Instalacionestipo','Instsupertipo','Atributo');
    var $helpers=array('Javascript','Ajax');
    var $paginate=array(
        'limit'=>'20',
        'order'=>array('Instalacionestipo.desc_tipo_inst'=>'ASC','Instsupertipo.supertipo_inst'=>'ASC')
    );

    function beforeFilter(){
        parent::beforeFilter();
        $this->__checkAdmin();
    }

    function listar(){
        $this->set('instalacionestipos',$this->paginate('Instalacionestipo'));
    }

    function agregar(){
        if (!empty($this->data)){
            if ($this->Instalacionestipo->save($this->data)){
                $id_inst=$this->Instalacionestipo->getID();
                //guardar atributos
                if(!empty($this->data['Atributo'])){
                    foreach($this->data['Atributo'] as $key=>$value){
                        $this->data['Atributo'][$key]['instalacionestipo_id']=$id_inst;
                        $this->Atributo->save($this->data['Atributo'][$key]);
                        $this->Atributo->create();
                    }
                }
                $this->Session->setFlash('Operacion realizada correctamente.');
                $this->redirect(array('action'=>'listar'),null,true);
            }else{
                $this->Session->setFlash('Error: No se pudo realizar la operacion.');
            }
        }
        $this->set('instsupertipos',$this->Instsupertipo->find('list',array(
                    'fields'=>'supertipo_inst',
                    'order'=>'supertipo_inst ASC'
                )));
        $this->Session->write('cont',0);

    }

    function editar($id=null){
        if(!empty($this->data)){
            if ($this->Instalacionestipo->save($this->data)){
                $id_instalacionestipo=$this->Instalacionestipo->getID();
                //guardar atributos
                if(!empty($this->data['Atributo'])){
                    foreach($this->data['Atributo'] as $key=>$value){
                        $this->data['Atributo'][$key]['instalacionestipo_id']=$id_instalacionestipo;
                        $this->Atributo->save($this->data['Atributo'][$key]);
                        $this->Atributo->create();
                    }
                }
                $this->Session->setFlash('Operacion realizada correctamente.');
                $this->redirect(array('action'=>'listar'),null,true);
            }else{
                $this->Session->setFlash('Error: No se pudo realizar la operacion.');
            }
        }
        if(empty($this->data)){
            $this->data=$this->Instalacionestipo->read(null,$id);
        }
        $this->set('instsupertipos',$this->Instsupertipo->find('list',array(
                    'fields'=>'supertipo_inst',
                    'order'=>'supertipo_inst ASC'
                )));
        $this->set('atributos',$this->Atributo->find('all',array(
                    'conditions'=>'Atributo.instalacionestipo_id='.$id,
                    'order'=>'Atributo.nombre_atr ASC'
                )));
        //funciona solo si hay menos de 999999 atributos
        $this->Session->write('cont',999999);
    }

    function eliminar($id=null){
        if($this->Instalacionestipo->del($id)){
            $this->Session->setFlash('Operacion realizada correctamente.');
            $this->redirect(array('action'=>'listar'),null,true);
        }else{
            $this->Session->setFlash('Error: No se pudo realizar la operacion.');
            $this->redirect(array('action'=>'listar'),null,true);
        }
    }

    function add_atributo(){
        Configure::write("debug",0);
        $x=$this->Session->read('cont')+1;
        $this->Session->write('cont',$x);
        $this->set("id", $x);
        $this->render('','','../elements/add_atributo');
    }

}
?>